package org.hxyjs.dao.impl;

import org.hxyjs.dao.ChildrenDao;
import org.hxyjs.entity.Children;
import org.hxyjs.entity.Children;
import org.hxyjs.entity.Children;
import org.hxyjs.mapper.ChildrenMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("childrenDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ChildrenDaoImpl implements ChildrenDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ChildrenMapper childrenMapper;
    public List<Children>  getAllChildrens() throws Exception{
        return childrenMapper.selectByExample(null);
    }

    @Override
    public Integer deleteChildrenByCno(String cno)  throws Exception{
        return childrenMapper.deleteByPrimaryKey(cno);
    }

    @Override
    public Integer updateChildrenByCno(Children children)  throws Exception{
        return childrenMapper.updateByPrimaryKey(children);
    }

    @Override
    public Integer addChildren(Children children)  throws Exception{
        children.setCno(initId());
        return childrenMapper.insertSelective(children);
    }
    public String initId(){
        String maxCno = childrenMapper.getMaxCno();
        return GenNumTools.initId("",3,maxCno);
    }
    @Override
    public List<Children> getChildrenByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)childrenMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return childrenMapper.getChildrenByPage(map);
    }
}
